ValidatedExpression

open class ValidatedExpression @JvmOverloads constructor(defaultValue: String, validVars: Set<Char> = setOf(), validator: EntryValidator<String> = object: EntryValidator<String> { override fun validateEntry(input: String, type: EntryValidator.ValidationType): ValidationResult<String> { return Expression.tryTest(input, validVars).wrap(input) } override fun toString(): String { return "Dummy test with valid variable chars" } }) : ValidatedField<String> , Expression

A validated math expression

This ValidatedField is itself an expression, so you can call eval() or evalSafe() on it directly

Author

fzzyhmstrs

Since

0.2.0

Parameters

defaultValue

String - representation of the desired math expression, parsed to a cached Expression internally.

validVars

Set - represents the valid variable characters the user can utilize in their expression.

validator

EntryValidator, optional - validates entered math strings

Throws

if the provided defaultValue is not a parsable Expression.

Samples

import me.fzzyhmstrs.fzzy_config.util.AllowableIdentifiers
import me.fzzyhmstrs.fzzy_config.util.EnumTranslatable
import me.fzzyhmstrs.fzzy_config.util.ValidationResult
import me.fzzyhmstrs.fzzy_config.validation.collection.ValidatedList
import me.fzzyhmstrs.fzzy_config.validation.minecraft.ValidatedIdentifier
import me.fzzyhmstrs.fzzy_config.validation.minecraft.ValidatedRegistryType
import me.fzzyhmstrs.fzzy_config.validation.minecraft.ValidatedTagKey
import me.fzzyhmstrs.fzzy_config.validation.misc.*
import me.fzzyhmstrs.fzzy_config.validation.misc.ValidatedColor.Companion.validatedColor
import me.fzzyhmstrs.fzzy_config.validation.number.ValidatedInt
import net.minecraft.item.Items
import net.minecraft.item.SwordItem
import net.minecraft.registry.Registries
import net.minecraft.registry.tag.ItemTags
import net.minecraft.util.Identifier
import java.awt.Color
import java.util.function.Function

fun main() { 
   //sampleStart 
   // example validated Expression; automatically parses and caches the Math Expression input in string form.
// The user can input any equation they like as long as it uses x, y, both, or neither expected variables passed in the set
val validatedExpression = ValidatedExpression("2.5 * x ^ 2 - 45 * y", setOf('x', 'y'))

fun evalExpressionExample() {
    val vars = mapOf('x' to 2.0, 'y' to 10.0) //prepared variable map with the current values of the expected vars
    val result = validatedExpression.eval(vars) // (= -440.0) straight eval() call. This can throw exceptions, so use with caution
    val resultSafe = validatedExpression.evalSafe(vars, -250.0) //when possible, use evalSafe with a fallback
}

//fields and sections have lang keys based on their "location" in the Config class graph.
//Lange key composition is as follows
//1. the namespace of the config id: (my_mod)
//2. the path of the config id: (my_mod.my_config)
//3. any parent ConfigSection field names as declared in-code: (my_mod.my_config.subSection)
//4. the setting field name as declared in-code: (my_mod.my_config.subSection.fieldName)
val fieldLang = """
{
    "_comment1": "the lang for an example 'fieldName' setting in a config inside section 'subSection'",
    "my_mod.my_config.subSection.fieldName": "Very Important Setting",
    "my_mod.my_config.subSection.fieldName.desc": "This very important setting is used in this very important way."
}
""" 
   //sampleEnd
}

Constructors

Link copied to clipboard
constructor()

A validated math expression with default equation of "0"

constructor(defaultValue: String, validVars: Set<Char> = setOf(), validator: EntryValidator<String> = object: EntryValidator<String> { override fun validateEntry(input: String, type: EntryValidator.ValidationType): ValidationResult<String> { return Expression.tryTest(input, validVars).wrap(input) } override fun toString(): String { return "Dummy test with valid variable chars" } })

Functions

Link copied to clipboard
open override fun accept(input: String)

updates the wrapped value. NOTE: this method will push updates to an UpdateManager, if any. For in-game updating consider validateAndSet

Link copied to clipboard

Attaches a listener to this field. This listener will be called any time the field is written to ("set"). accept, validateAndSet, setAndUpdate and so on will all call the listener.

Link copied to clipboard
open fun andThen(p0: Consumer<in String>): Consumer<String>
Link copied to clipboard
fun codec(): Codec<String>

Provides a Codec representing the value type of this validation, backed by the validators within, as applicable

Link copied to clipboard
open override fun copyStoredValue(): String

Creates a deep copy of the stored value and returns it

Link copied to clipboard
open fun description(fallback: String? = null): MutableText

The translated Text description from the descriptionKey. Falls back to an empty string so no tooltip is rendered.

Link copied to clipboard
open override fun descriptionKey(): String

translation key of this Translatable's description. the "description" in-game, the descriptions Enchantment Descriptions adds to enchantment tooltips are a good example.

Link copied to clipboard
open override fun eval(vars: Map<Char, Double>): Double

Evaluates the math expression

Link copied to clipboard
open fun evalSafe(vars: Map<Char, Double>, fallback: Double): Double

Evaluates an expression with a fallback. Will fail to fallback instead of throwing. This call is recommended over the 'raw' eval call

Link copied to clipboard
open override fun get(): String

supplies the wrapped value

Link copied to clipboard

Provides this validations default value

Link copied to clipboard

Whether this Translatable has a valid description

Link copied to clipboard

Whether this Translatable has a valid translation

Link copied to clipboard
open override fun instanceEntry(): ValidatedExpression

creates a deep copy of this ValidatedExpression return ValidatedExpression wrapping a deep copy of the currently stored expression, valid variable, and validation

Link copied to clipboard
fun <N> map(to: Function<String, out N>, from: Function<in N, String>): ValidatedField<N>

Maps this validation to a new convertible type. The default value will be applied from this delegates current storedValue

fun <N> map(defaultValue: N, to: Function<String, out N>, from: Function<in N, String>): ValidatedField<N>
fun <N> map(to: Function<String, out N>, from: Function<in N, String>, defaultValue: String): ValidatedField<N>

Maps this validation to a new convertible type.

Link copied to clipboard
fun toList(vararg elements: String): ValidatedList<String>

wraps the provided values into a ValidatedList with this field as validation

wraps the provided collection into a ValidatedList with this field as validation

Link copied to clipboard
fun toSet(vararg elements: String): ValidatedSet<String>

wraps the provided values into a ValidatedSet with this field as validation

wraps the provided collection into a ValidatedList with this field as validation

Link copied to clipboard
open override fun translation(fallback: String?): MutableText

The translated Text name from the translationKey. Falls back to the implementing classes Simple Name (non-translated)

Link copied to clipboard
open override fun translationKey(): String

translation key of this Translatable. the "name" in-game

Link copied to clipboard
fun trySerialize(input: Any?, errorBuilder: MutableList<String>, flags: Byte): TomlElement?
Link copied to clipboard
open override fun trySet(input: Any?)
Link copied to clipboard
open fun update(updateMessage: Text)
Link copied to clipboard

A setter method for the storedValue that first validates the value being set and then stores the post-validation result.

Link copied to clipboard
open fun widgetAndTooltipEntry(choicePredicate: ChoiceValidator<String> = ChoiceValidator.any()): ClickableWidget